# -------------------------------------------------------------------
#       -*- coding: utf-8 -*-
#   @Project    :   spider
#   @File       :   24_xpath.py
#   @Author     :   WANGYU
#   @Time       :   2021-08-18 09:41:25
#   @Software   :   PyCharm
#   @Desc       :   
# -------------------------------------------------------------------


# xpath 是在xml文档中搜索内容到一门语言
# html是xml到一个子集
# 安装lxml模块
from lxml import html
# from lxml.html import etree

xml = '''
    <book>
        

    </book>
'''

tree = html.etree.XML(xml)
# result = tree.xpath('/book') #/表示层级关系，第一个/是根节点
# result = tree.xpath('/book/name')
# result = tree.xpath('/book/name/text()') # text()拿文本

result = tree.xpath('/book/author/nick//text()')  # //表示author内所有后代nick
result = tree.xpath('/book/author/nick/*/text()')  # /*/表示任意节点，通配符

result = tree.xpath("/html/body/ol/li[1]/a")
result = tree.xpath("/html/body/ol/li/a[@href='dapao']")

print(result)

ol_li_list = tree.xpath('/html/body/ol/li')

for li in ol_li_list:
    # 从每个li中提取文字信息
    result = li.xpath('./a/text()')  # 在li中继续去寻找，相对查找
    print(result)
    result2 = li.xpath('./a/@herf')  # 拿到属性值，@属性名

print(tree.xpath('/html/body/ol/li/a/@href'))

# /html/body/div[3]/div[4]/div/a[3]/div[2]/p
